﻿using System;
using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;

namespace HotUpdate
{
    public class LoadAB : MonoBehaviour
    {
        private void Awake()
        {
            Debug.Log(transform.position);
        }

        public string path = "HotRes/HotScene";

        IEnumerator Start()
        {
            AssetBundle ab = null;
            yield return LoadABRes(path, ab);
            yield return 0;
            SceneManager.LoadScene("HotScene", LoadSceneMode.Single);
            yield return 0;

            ab.Unload(false);
        }

        IEnumerator LoadABRes(string path, AssetBundle ab, string ex = ".unity3d")
        {
            string newPath = $"{path.ToLower()}{ex}";
#if UNITY_STANDALONE_WIN || UNITY_EDITOR
            ab = AssetBundle.LoadFromFile($"{Application.streamingAssetsPath}/{newPath}");
#elif UNITY_WEBGL
            var web = UnityWebRequestAssetBundle.GetAssetBundle($"StreamingAssets/{newPath}?ran={Time.realtimeSinceStartup}");
            yield return web.SendWebRequest();
            Debug.Log($"webgl load done:{web.error}");
            ab = DownloadHandlerAssetBundle.GetContent(web);
#endif
            yield return 0;
        }
    }
}